<template>
  <div class="app-container">
    <el-form :model="teacherForm" :rules="rules" ref="teacherForm" label-width="100px" class="demo-teacherForm">
      <el-form-item label="讲师名称" prop="name">
        <el-input v-model="teacherForm.name"></el-input>
      </el-form-item>
      <el-form-item label="头衔" prop="level">
        <el-select v-model="teacherForm.level" placeholder="请选择头衔">
          <el-option label="高级讲师" value="1"></el-option>
          <el-option label="首席讲师" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="简介" prop="intro">
        <el-input type="textarea" v-model="teacherForm.intro"></el-input>
      </el-form-item>


      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="teacherForm.sort"  ></el-input-number>

      </el-form-item>
      <el-form-item label="资历" prop="career">
        <el-input v-model="teacherForm.career"/>
      </el-form-item>

      <el-form-item label="头像" prop="avatar">
        <pan-thumb :image="teacherForm.avatar" />

        <el-button type="primary" icon="el-icon-upload" style="position: absolute;bottom: 15px;margin-left: 40px;"
                   @click="imagecropperShow = true">
          上传图片
        </el-button>

        <image-cropper
          v-show="imagecropperShow"
          :key="imagecropperKey"
          :width="300" :height="300"
          :url="BASE_API+'/teacher/uploadAvatar'"
          field="file"
          lang-type="en"
          @close="close"
          @crop-upload-success="cropSuccess" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('teacherForm')">立即添加</el-button>
        <el-button @click="resetForm('teacherForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { save,getById } from '@/api/teacher'
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

export default {
  name: 'Save',
  components: { ImageCropper, PanThumb },

  data() {

    return {
      BASE_API:process.env.BASE_API,
      imagecropperShow: false,
      imagecropperKey: 0,
      teacherForm: {
        name: '',
        sort:1,
        level: '',
        career: '',
        intro: '',
        avatar: 'https://wpimg.wallstcn.com/577965b9-bb9e-4e02-9f0c-095b41417191',

      },
      rules: {
        name: [
          { required: true, message: '请输入讲师名称', trigger: 'blur' },

        ],
        level: [
          { required: true, message: '请选择头衔', trigger: 'change' }
        ],
        intro: [
          { required: true, message: '请输入简介', trigger: 'change' }
        ],

        career: [
          { required: true, message: '请输入资历', trigger: 'change' }
        ],
        avatar: [
          { required: true, message: '请上传头像', trigger: 'change' }
        ],


      }
    };
  },
  created() {
    if (this.$route.query.id !== undefined) {
      getById(this.$route.query.id).then((res) => {
        this.teacherForm = res.data
      })
    }
  },
  methods: {
    cropSuccess(resData) {
      console.log(resData)
      this.imagecropperShow = false
      this.imagecropperKey = this.imagecropperKey + 1
      this.teacherForm.avatar = resData
    },
    close() {
      this.imagecropperShow = false
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {//校验成功
          save(this.teacherForm).then(response => {
            //跳到讲师列表
            this.$message.success("添加成功")

            this.$router.push({ path: '/teacher/list' })
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }

}
</script>

<style></style>
